<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      :columns="columns"
      :request-api="getTableList"
      :init-param="initParam"
      :data-callback="dataCallback"
      @drag-sort="sortTable"
      :pagination="false"
    >
    </ProTable>
  </div>
</template>

<script setup lang="tsx" name="deviceList">
import { ref, reactive } from "vue";
import { User } from "@/api/interface";
import { ElMessage } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import ShowChileDate from "@/components/ShowChileDate/index.vue";
import { getDeviceListApi } from "@/api/modules/datawarn";

const proTable = ref<ProTableInstance>();
// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({
  pageNum: 1,
  pageSize: 10
});

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total 这些字段，可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
  return data.list;
};

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
  let newParams = JSON.parse(JSON.stringify(params));
  return getDeviceListApi(newParams);
};
// 表格配置项
const columns = reactive<ColumnProps<User.ResUserList>[]>([
  { prop: "id", label: "设备ID", width: 80 },
  { prop: "shop_id", label: "店铺ID", width: 80 },
  {
    prop: "pda_num",
    label: "设备码"
  },
  {
    prop: "remark",
    label: "备注",
    width: 100
  },
  {
    prop: "last_active_time",
    label: "更新时间",
    width: 180,
    render: (scope: any) => {
      return <ShowChileDate value={scope.row.last_active_time} />;
    }
  },
  {
    prop: "create_time",
    label: "创建时间",
    width: 180
  }
]);

// 表格拖拽排序
const sortTable = ({ newIndex, oldIndex }: { newIndex?: number; oldIndex?: number }) => {
  console.log(newIndex, oldIndex);
  console.log(proTable.value?.tableData);
  ElMessage.success("修改列表排序成功");
};
</script>

<style scoped>
.order-container {
  padding: 12px;
  margin-bottom: 20px;
  border-radius: 4px;
}
.positive-order {
  background-color: #f0f9eb;
  border: 1px solid #e1f3d8;
}
.negative-order {
  background-color: #fef0f0;
  border: 1px solid #fde2e2;
}
.order-title {
  margin-bottom: 10px;
  font-weight: bold;
  color: #333333;
}
.goods-item {
  padding: 8px 0;
  border-bottom: 1px dashed #ebeef5;
}
.goods-item:last-child {
  border-bottom: none;
}
.goods-name {
  margin-bottom: 4px;
  color: #606266;
}
.goods-price {
  font-size: 13px;
  color: #909399;
}
</style>
